﻿#Include ..\baseHandle.ahk

#Include ..\..\_lib\Fs.ahk

class Code extends BaseHandle {

  static Handle(parsed) {
    t := parsed.target, p := parsed.params.Length ? '-' parsed.params[1] : ''
    if p and (p != '-r' and p != '-n')
      return this.Fail('无效的参数：' p)
    cfs := MeowConf.Of('./cfg/codeFile.txt')
    if r := cfs.Get(parsed.target) {
      p := p || (Fs.IsDir(r) ? '-n' : '-r')  ; 如果没有指定，则根据是否为文件夹设置
      Run(A_ComSpec ' /c code ' JoinStr(A_Space, p, '"' r '"', ' && exit'), , 'hide')
      return this.Succ('ok, open custom file', 'x')
    }
    if FileExist(t) {
      Run(A_ComSpec ' /c code ' t ' && exit', , 'hide')
      return this.Succ('ok', 'x')
    }
    return this.Fail('无效的执行目标' t)
  }

  static Spec() {
    return {
      cmd: 'code',
      desc: '使用vscode打开',
      format: 'code [-r|-n] <target>',
      params: [{
        name: 'r',
        desc: '重用窗口'
      }, {
        name: 'n',
        desc: '新开窗口'
      }],
      kvparams: [],
      args: {
        desc: '',
        isOptional: false,
        cacheable: true,
        generator: {
          script: '',
          postProcess: (*) => MeowConf.Of('./cfg/codeFile.txt').data.keys
        }
      },
      extra: {
        isEnable: false
      }
    }
  }

}
